/**
 * 
 */
$(function() {
	var shopId = getQueryString('shopId');
	var isEdit = shopId?true:false;
	
	var initUrl = '/o2o/shopadmin/getshopinitinfo';
	var registerShopUrl = '/o2o/shopadmin/registershop';
	var shopInfoUrl = '/o2o/shopadmin/getshopbyid?shopId=' + shopId;
	var editShopUrl = '/o2o/shopadmin/modifyshop?';
	if(!isEdit){
		getShopInitInfo();
	} else {
		getShopInfo(shopId);
	}
	
	function getShopInfo(shopId){
		$.getJSON(shopInfoUrl, function(data){
			if(data.success){
				var shop = data.shop
				$('#shop-name').val(shop.shopName);
				$('#shop-addr').val(shop.shopAddr);
				$('#shop-phone').val(shop.phone);
				$('#shop-desc').val(shop.shopDesc);
				
				var shopCategory = '<option data-id="' + shop.shopCategory.shopCategoryId
							+ '">' + shop.shopCategory.shopCategoryName + '</option>';
				var tempAreaHtml = '';
				data.areaList.map(function(item, index) {
					tempAreaHtml += '<option data-id="' + item.areaId + '">'
							+ item.areaName + '</option>';
				});
				
				$('#shop-category').html(shopCategory);
				$('#shop-category').attr('disabled','disabled');
				$('#area').html(tempAreaHtml);
				$("#area option[data-id='" + shop.area.areaId + "']").attr("selected","selected");
			}
		});
	}
	
	function getShopInitInfo() {
		$.getJSON(initUrl, function(data) {
			if (data.success) {
				var tempHtml = '';
				var tempAreaHtml = '';
				data.shopCategoryList.map(function(item, index) {
					tempHtml += '<option data-id="' + item.shopCategoryId
							+ '">' + item.shopCategoryName + '</option>';
				});

				data.areaList.map(function(item, index) {
					tempAreaHtml += '<option data-id="' + item.areaId + '">'
							+ item.areaName + '</option>';
				});

				$('#shop-category').html(tempHtml);
				$('#area').html(tempAreaHtml);
			}
		});
	}
	
	function checkPhone(phone){
		
		var mobileRegex=/^1[34578]\d{9}$/;
		var telRegex=/^0\d{2,3}-?\d{7,8}$/;
		if(mobileRegex.test(phone) || telRegex.test(phone)){
			return true;
		}
		
		return false;
	}
	
	$('#submit').click(
		function() {
			
			var shop = {};
			if(isEdit){
				shop.shopId = shopId; 
			}
			shop.shopName = $('#shop-name').val();
			shop.shopAddr = $('#shop-addr').val();
			shop.phone = $('#shop-phone').val();
			shop.shopDesc = $('#shop-desc').val();
			shop.shopCategory = {
				shopCategoryId : $('#shop-category').find('option')
						.not(function() {
							return !this.selected;
						}).data('id')
			};
			shop.area = {
				areaId : $('#area').find('option').not(function() {
						return !this.selected;
					}).data('id')
			};

			var shopImg = $('#shop-img')[0].files[0];
			var verifyCodeActual = $("#kaptchaCode").val();
			if(!verifyCodeActual){
				$.toast('验证码不能为空！');
				return false;
			}
			
			//表单校验
			if(!checkPhone(shop.phone)){
				$.alert("请输入正确的联系电话!", "提示");
				return false;
			}
			if(!shop.shopName || !shop.shopAddr || !shop.shopDesc){
				$.alert("请填写完整！", "提示");
				return false;
			}
			
			
			var formData = new FormData();
			formData.append('shopImg', shopImg);
			formData.append('shopStr', JSON.stringify(shop));
			formData.append('verifyCodeActual',verifyCodeActual);
			(function(){
				$.ajax({
					url : (isEdit? editShopUrl :registerShopUrl),
					type : 'POST',
					data : formData,
					contentType : false,
					processData : false,
					cache : false,
					success : function(data){
						if(data.success){
							$.toast('提交成功！');
							window.location.href = getContextPath() + '/shopadmin/shoplist';
						} else {
							$.toast('提交失败！' + data.errMsg);
						}
						
						$('#kaptcha_img').click();
					}
				});
			})();
			
		});
})